import Vue from 'vue'
import Vuex from 'vuex'
import shoppingCart from "../http/shoppingCartApi"
import order from "../http/order"

Vue.use(Vuex)

export default new Vuex.Store({
  state: { // 存在数据的仓库
    user:null,
    showLogin:false,  // 登录组件是否展示
    shopcartList:[]  // 购物车数据
  },
  getters:{ // 相当于计算属性
    userInfo(state){
      return state.user
    },
    userName(state,getters){
      return getters.userInfo?getters.userInfo.userName:""
    },
    getShowLogin(state){
      return state.showLogin
    },
    getShopCart(state){ //获取购物车所有的数据
      return state.shopcartList
    },
    getCkAll(state){ // 获取全选
      let flag = state.shopcartList.every(item=>item.check)
      return flag
    },
    getAllNum(state){ // 获取所有的数量
      let num =0
      state.shopcartList.forEach(item=>{
        num+=item.num
      })
      return num
    },
    getAllCheckGoods(state){ // 获取选中的商品
      let num =0
      state.shopcartList.forEach(item=>{
        if(item.check){
          num+=item.num
        }
      })
      return num
    },
    getAllTotal(state){ // 获取总价
      let num = 0
      state.shopcartList.forEach(item=>{
        if(item.check){
          num+=item.num*item.price
        }
      })
      return num
    },
    showGoShop(state){ // 判断子复选框是否存在选中的，存在就让 去结算 显示
      let flag = state.shopcartList.some(item=>item.check)
      return flag
    },
    getCheckShopList(state){ // 过滤出选中的商品
      return state.shopcartList.filter(item=>item.check)
    }
  },
  mutations: {  // 唯一可以修改state数据的方法集合
    setUser(state,data){
      state.user=data
    },
    setShowlogin(state,data){
      state.showLogin=data;
    },
    setShopCart(state,data){
      state.shopcartList=data
    },
    setCkAll(state,bool){
      state.shopcartList.forEach(item=>item.check=bool)
    },
    setUpdateShopCartCheck(state,obj){
      let index =state.shopcartList.findIndex(item=>item.productID==obj.product_id)
      state.shopcartList[index].num = obj.num
      state.shopcartList[index].check=true
    },
    delHandle(state,id){
      let index =state.shopcartList.findIndex(item=>item.productID==id)
      state.shopcartList.splice(index,1)
    }
  },
  actions: { // 处理异步操作的
    setShopCart({commit},payload){
      commit('setShopCart',payload)
    },
    setCkAll({commit},bool){
      commit('setCkAll',bool)
    },
    setUpdateShopCartCheck({commit},payload){
      commit('setUpdateShopCartCheck',payload)
    },
    async setUpdateShopCartNum(ctx,payload){ // 向后端发送修改num操作
      let  obj ={
        user_id:ctx.state.user.user_id,
        product_id:payload.productID,
        num:payload.num,
      }
        let {data:res} = await shoppingCart.updateShoppingCart(obj)
        if(res.code === '001'){
          Vue.prototype.$notifySuccess(res.msg)
          ctx.commit('setUpdateShopCartNum',obj)
        }
    },
    async delHandle(ctx,id){ // 向后端发送删除操作
      let obj ={
        user_id:ctx.state.user.user_id,
        product_id:id,
      }
      let {data:res} = await shoppingCart.deleteShoppingCart(obj)
      if(res.code==='001'){
        Vue.prototype.$notifySuccess(res.msg)
        ctx.commit('delHandle',id)
      }
    },
    async toShop(ctx){  // 向后端发送结算，结算完需要跳转订单页面
      let obj ={
        user_id:ctx.state.user.user_id,
        products:ctx.getters.getCheckShopList,
      }
      let {data:res} = await order.addOrder(obj)
      console.log(res);
    }
  },
  modules: {
  }
})
